package com.leyou.search.listener;

import com.leyou.common.constants.MQConstants;
import com.leyou.search.service.SearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SearchListener {

    @Autowired
    private SearchService searchService;

    /**
     * 添加索引库监听
     * 在消息的消费方，来指定当前监听的队列的名称，队列绑定的交换机的名词，队列与交换机之间绑定的路由名称
     */
    @RabbitListener(bindings=@QueueBinding(
            value = @Queue(name = MQConstants.Queue.SEARCH_ITEM_UP, durable="true"),
            exchange = @Exchange(name = MQConstants.Exchange.ITEM_EXCHANGE_NAME, type = ExchangeTypes.TOPIC),
            key = MQConstants.RoutingKey.ITEM_UP_KEY
    ))
    public void addIndex(Long id){
        searchService.addIndex(id);
    }

    /**
     * 删除索引库监听
     */
    @RabbitListener(bindings=@QueueBinding(
            value = @Queue(name = MQConstants.Queue.SEARCH_ITEM_DOWN, durable="true"),
            exchange = @Exchange(name = MQConstants.Exchange.ITEM_EXCHANGE_NAME, type = ExchangeTypes.TOPIC),
            key = MQConstants.RoutingKey.ITEM_DOWN_KEY
    ))
    public void delIndex(Long id){
        searchService.delIndex(id);
    }

}
